package com.qf.fmall2203.shopcart.controller;


import com.qf.fmall2203.alipay.WxpayUtils;
import com.qf.fmall2203.orders.entity.Orders;
import com.qf.fmall2203.shopcart.entity.OrderVo;
import com.qf.fmall2203.shopcart.service.IShoppingCartService;
import com.qf.fmall2203.utils.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/order")
@CrossOrigin
@Slf4j
public class OrderController {

    @Autowired
    IShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public ResultVo addOrder(@RequestBody Orders orders, Integer[] cids) throws Exception {

        log.debug("orders={},cids={}",orders,cids);

        HashMap<String, Object> map = shoppingCartService.addOrder(orders,cids);

        return ResultVo.ok(map);

    }

    @GetMapping("/status/{orderId}")
    public ResultVo queryOrderPay(@PathVariable("orderId") String orderId) throws Exception {

        String answer = WxpayUtils.queryOrder(orderId);

        if(answer.equals("SUCCESS")){
            return ResultVo.ok(2);
        }else {
            return ResultVo.ok(-2);
        }
    }
}
